<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Robot Terminal</title>
    <style>
        /* 设置输入框和发送按钮的样式 */
        .container {
            display: flex;
            flex-direction: row;
            flex-grow: 1;
        }

        .controls {
            display: flex;
            /* 使用 Flex 布局 */
            align-items: center;
            /* 垂直居中对齐 */
            justify-content: flex-start;
            /* 靠左对齐 */
            margin-top: 10px;
        }

        .blue-text {
            color: blue;
        }

        input {
            width: 100%;
            padding: 10px;
        }

        button {
            padding: 10px;
            background-color: #007BFF;
            width: 100px;
            color: white;
            border: none;
            cursor: pointer;
            margin-left: 10px;
            flex-grow: 1;
            font-size: 24px;
            /* 您可以根据需要更改此字体大小 */
            font-weight: bold;
            /* 您可以根据需要添加此加粗样式 */
            border-radius: 10px;
        }

        label {
            display: block;
            margin-left: 10px;
        }

        pre {
            font-size: 2px;
            /* font-family: 'ANSI Shadow', sans-serif; */
            /* 设置字体大小为 16 像素 */
        }

        #auto-scroll {
            margin-left: 10px;
            width: auto;
        }

        #clear-button {
            padding: 3px;
            background-color: rgb(119, 81, 25);
            border: 1px solid rgb(93, 30, 30);
            width: auto;
            font-size: 16px;
            margin-right: 10px;
            margin-left: auto;
            flex-grow: 0;
            border-radius: 10px;
        }

        /* 设置输出框的样式 */
        #output {
            width: 96%;
            height: 200px;
            overflow-y: scroll;
            border: 1px solid black;
            margin-right: 10px;
            padding: 10px;
        }

        @media (max-width: 600px) {
            .container {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <!-- <h1>Robot Terminal</h1> -->
    <pre>
██████╗  ██████╗ ██████╗  ██████╗ ████████╗    ████████╗███████╗██████╗ ███╗   ███╗██╗███╗   ██╗ █████╗ ██╗     
██╔══██╗██╔═══██╗██╔══██╗██╔═══██╗╚══██╔══╝    ╚══██╔══╝██╔════╝██╔══██╗████╗ ████║██║████╗  ██║██╔══██╗██║     
██████╔╝██║   ██║██████╔╝██║   ██║   ██║          ██║   █████╗  ██████╔╝██╔████╔██║██║██╔██╗ ██║███████║██║     
██╔══██╗██║   ██║██╔══██╗██║   ██║   ██║          ██║   ██╔══╝  ██╔══██╗██║╚██╔╝██║██║██║╚██╗██║██╔══██║██║     
██║  ██║╚██████╔╝██████╔╝╚██████╔╝   ██║          ██║   ███████╗██║  ██║██║ ╚═╝ ██║██║██║ ╚████║██║  ██║███████╗
╚═╝  ╚═╝ ╚═════╝ ╚═════╝  ╚═════╝    ╚═╝          ╚═╝   ╚══════╝╚═╝  ╚═╝╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝
    </pre>
    <div class="container">
        <input type="text" id="inputTerminal" placeholder="请输入文本">
        <button onclick="sendText()">发送</button>
    </div>
    <p id="output"></p>

    <div class="controls">
        <label for="auto-scroll">自动跳转行尾</label>
        <input type="checkbox" id="auto-scroll" onchange="toggleAutoScroll()" />
        <button onclick="clearOutput()" id="clear-button">清空文本框</button>
    </div>



    <script>
        function jumpToEnd() {
            document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight;
        }
        function toggleAutoScroll() {
            if (document.getElementById('auto-scroll').checked) {
                // 复选框被选中，执行跳转行尾操作
                jumpToEnd();
            }
        }
        function clearOutput() {
            const output = document.getElementById('output');
            output.innerHTML = ''; // 清空输出框内容
        }
        function sendText() {
            const inputTerminal = document.getElementById('inputTerminal');
            const output = document.getElementById('output');
            const text = inputTerminal.value;
            const now = new Date();
            const timestamp = now.toLocaleTimeString();
            var currentURL = window.location.href;

            if (text) {
                output.innerHTML += `<span class="blue-text">[${timestamp}]<- ${text}</span><br>`;
                inputTerminal.value = '';
                sendAjax(text);
                if (document.getElementById('auto-scroll').checked) { jumpToEnd(); }
            }
        }
        function sendAjax(text) {
            fetch('http://localhost:8080/terminal', {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain'
                },
                body: text
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.text(); // 返回文本数据
                })
                .then(data => {
                    console.log('Success:', data);

                })
                .catch((error) => {
                    console.error('Error:', error);
                });
        }
        document.addEventListener('keydown', function (event) {
            if (event.key === 'Enter') {
                sendText();
            }
        });
        function updateMSG() {
            const text = '?';
            const now = new Date();
            const timestamp = now.toLocaleTimeString();
            fetch('http://localhost:8080/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain'
                },
                body: text
            })
                .then(response => response.text())
                .then(data => {
                    // 更新页面上的局部内容以显示响应数据
                    output.innerHTML += `[${timestamp}]-> ${data}<br>`;
                    if (document.getElementById('auto-scroll').checked) { jumpToEnd(); }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
        setInterval(updateMSG, 1000);
    </script>
</body>

</html>